package com.chezhujie.www.service;

import com.chezhujie.www.core.exception.ClassCheckException;
import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjcommoditytype;
import com.chezhujie.www.persistence.CzjcommentsMapper;
import com.chezhujie.www.persistence.CzjcommoditytypeMapper;
import com.chezhujie.www.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 商品类型表
 * Created by 汤伟杰 on 2017/5/23.
 */
@Service
public class CzjcommoditytypeService extends BaseService<Czjcommoditytype>{
    @Autowired
    private CzjcommoditytypeMapper czjcommoditytypeMapper;

    @Autowired
    public void setBaseMapper(CzjcommoditytypeMapper czjcommoditytypeMapper) {
        this.baseMapper = czjcommoditytypeMapper;
    }


    public Pagination list(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) throws Exception {

        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = czjcommoditytypeMapper.getTotalRows(params);
        List<Map<String, Object>> list = czjcommoditytypeMapper.getList(params);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;
    }


    public void insert(Czjcommoditytype czjcommoditytype){
        if(StringUtils.isNotBlank(czjcommoditytype.getId())){
        czjcommoditytype.setId(UUIDUtils.getUUID());
        }
        czjcommoditytypeMapper.insert(czjcommoditytype);
    }

    public void update(Czjcommoditytype czjcommoditytype){
        czjcommoditytypeMapper.update(czjcommoditytype);
    }
    public void deleteById(String id){
        czjcommoditytypeMapper.deleteById(id);
    }
    public void deleteByIds(List<String> list){
        czjcommoditytypeMapper.deleteByIds(list);
    }
    public Czjcommoditytype queryById(String id){
        return czjcommoditytypeMapper.queryById(id);
    }
    public List<Czjcommoditytype> queryByIds(List<String> list){
        List<Czjcommoditytype> listCzjcommoditytype = czjcommoditytypeMapper.queryByIds(list);
        if(listCzjcommoditytype==null){
            listCzjcommoditytype = new ArrayList<Czjcommoditytype>();
        }
        return listCzjcommoditytype;
    }
    public List<Map<String,Object>> getListSupId(Map<String, Object> param) {
        return czjcommoditytypeMapper.listSupId(param);
    }

    public void checkEntity(Czjcommoditytype czjcommoditytype) throws Exception {
            if(czjcommoditytype.getTypeName().length()<19){
                throw new ClassCheckException("商品类型表类别不能超过19个字符");
            }
            if(czjcommoditytype.getRemark().length()<100){
                throw new ClassCheckException("商品类型表备注不能超过100个字符");
            }


    }

    public Pagination getListByHasCommodity() {
        Pagination pagination = new Pagination();

        List<Map<String, Object>> list = czjcommoditytypeMapper.getListByHasCommodity();
        pagination.setList(list);
        return pagination;
    }
}
